<?php

namespace app\api\controller\v1;

use app\admin\model\Customeraddress;

class Address extends Base
{
    public function showDefaultAddress()
    {
        $data = Customeraddress::where('fk_bdl_bd_customer',$this->customer_id)->where('fk_bdl_isdefault',1)->find();

        if(!$data)
        {
            $this->success('暂无默认地址',[]);
        }

        $this->success('获取成功',$data);

    }
    public function cusAddress()
    {
        $data = Customeraddress::where('fk_bdl_bd_customer',$this->customer_id)->select();

        $this->success('获取成功',$data);
    }

    public function addAddress()
    {
        $check      = $this->plulicValidate($this->params,
            ['fk_bdl_bd_cusaddress|详细地址', 'bdl_contacts|真实姓名','fk_bdl_phone|联系电话','fk_bdl_province|省','fk_bdl_city|市','fk_bdl_region|区','fk_bdl_isdefault|是否默认'],
            ['require',  'require',  'require','require',  'require',  'require',  'require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }

        if($this->params['fk_bdl_isdefault'] == 1)
        {
            Customeraddress::where('fk_bdl_bd_customer',$this->customer_id)->update(['fk_bdl_isdefault'=>0]);
        }

        $data = [
            'fk_bdl_bd_cusaddress'      => $this->params['fk_bdl_bd_cusaddress'],
            'bdl_contacts'              => $this->params['bdl_contacts'],
            'fk_bdl_phone'              => $this->params['fk_bdl_phone'],
            'fk_bdl_province'           => $this->params['fk_bdl_province'],
            'fk_bdl_city'               => $this->params['fk_bdl_city'],
            'fk_bdl_region'             => $this->params['fk_bdl_region'],
            'fk_bdl_bd_customer'        => $this->customer_id,
            'forgid'                    => $this->forgid,
            'fk_bdl_bd_datacenter'      => $this->datacenter,
            'fk_bdl_isdefault'          => $this->params['fk_bdl_isdefault'] == 1 ? 1 : 0,
        ];

        $res = Customeraddress::create($data);

        if(!$res)
        {
            $this->error('添加失败');
        }
        $this->success('添加成功');
    }

    public function editAddress()
    {
        $check      = $this->plulicValidate($this->params,
            ['fk_bdl_bd_cusaddress|详细地址', 'bdl_contacts|真实姓名','fk_bdl_phone|联系电话','fk_bdl_province|省','fk_bdl_city|市','fk_bdl_region|区','id|ID','fk_bdl_isdefault|是否默认'],
            ['require',  'require',  'require','require',  'require',  'require',  'require',  'require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }

        if($this->params['fk_bdl_isdefault'] == 1)
        {
            Customeraddress::where('fk_bdl_bd_customer',$this->customer_id)->update(['fk_bdl_isdefault'=>0]);
        }

        $data = [
            'fk_bdl_bd_cusaddress'      => $this->params['fk_bdl_bd_cusaddress'],
            'bdl_contacts'              => $this->params['bdl_contacts'],
            'fk_bdl_phone'              => $this->params['fk_bdl_phone'],
            'fk_bdl_province'           => $this->params['fk_bdl_province'],
            'fk_bdl_city'               => $this->params['fk_bdl_city'],
            'fk_bdl_region'             => $this->params['fk_bdl_region'],
            'fk_bdl_bd_customer'        => $this->customer_id,
            'fk_bdl_isdefault'          => $this->params['fk_bdl_isdefault'] == 1 ? 1 : 0,

        ];

        $res = Customeraddress::where('id',$this->params['id'])->update($data);

        if(!$res)
        {
            $this->error('修改失败');
        }
        $this->success('修改成功',$data);
    }

    public function delAddress()
    {
        $check      = $this->plulicValidate($this->params,
            ['id|ID'],
            ['require']);

        if ($check['code'] == 1)
        {
            $this->error('缺少必要值');
        }
        $model      = Customeraddress::get($this->params['id']);

        $res        = $model->delete();

        if(!$res)
        {
            $this->error('删除失败');
        }

        $this->success('删除成功');
    }

    public function defaultAddress()
    {
        $check      = $this->plulicValidate($this->params,
            ['id|ID'],
            ['require']);

        if ($check['code'] == 1)
        {
            $this->error('缺少必要值');
        }

        $res        = Customeraddress::where('fk_bdl_bd_customer',$this->customer_id)->update(['fk_bdl_isdefault'=>0]);

        if($res)
        {
            $res    = Customeraddress::where('id',$this->params['id'])->update(['fk_bdl_isdefault'=>1]);

            if($res)
            {
                $this->success('设置成功');
            }
        }

        $this->error('设置失败');
    }

    public function getAddress()
    {
        $check      = $this->plulicValidate($this->params,
            ['id|ID'],
            ['require']);

        if ($check['code'] == 1)
        {
            $this->error('缺少必要值');
        }

        $data = Customeraddress::get($this->params['id']);

        $this->success('获取成功',$data);
    }

}